package spring.yongongsystem.controller.admin;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import spring.yongongsystem.POJO.dto.AdminDTO;
import spring.yongongsystem.POJO.dto.CategoryPageQueryDTO;
import spring.yongongsystem.POJO.entity.Category;
import spring.yongongsystem.result.PageResult;
import spring.yongongsystem.result.Result;
import spring.yongongsystem.service.CategoryService;
@CrossOrigin(origins = "http://localhost:5173")
@RestController("adminCategoryController")
@RequestMapping("/admin/category")
@Api(tags = "管理员分类相关接口")
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     * @param categories
     * @return
     */
    @PostMapping
    @ApiOperation("新增分类")
    public Result<String> save(@RequestBody Category categories){
        log.info("新增分类：{}", categories);
        categoryService.save(categories);
        return Result.success();
    }

    /**
     * 分类分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分类分页查询")
    public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("分页查询：{}", categoryPageQueryDTO);
        PageResult pageResult = categoryService.pageQuery(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除分类")
    public Result<String> deleteById(Long id){
        log.info("删除分类：{}", id);
        categoryService.deleteById(id);
        return Result.success();
    }

    /**
     * 编辑分类信息
     * @param category
     * @return
     */
    @PutMapping
    @ApiOperation("编辑分类信息")
    public Result update(@RequestBody Category category){
        log.info("编辑分类信息：{}", category);
        categoryService.update(category);
        return Result.success();
    }
}
